﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 18/01/2009
// Time: 20:29
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;

namespace csharp.rad.general.entries
{
	/// <summary>
	/// Description of frmRegisterCompany.
	/// </summary>
	public partial class frmRegisterCompany : frmRegisterPerson
	{
		Company company = new Company();
		Companies listCompany = new Companies();
		
		public frmRegisterCompany()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			this.dgvMain.Size = new Size(808, 426);
			
			base.person = company.person;
		}
		
		#region overrided methods
		protected override bool delete()
		{
			return base.delete();
		}
		
		protected override void fillDataGridView()
		{
			//base.fillDataGridView();
			
			listCompany.fillList();
			
			int tam= listCompany.count();
			
			dgvMain.Rows.Clear();
			for (int i=0; i < tam; i++)
				dgvMain.Rows.Add(new String[] {listCompany.get(i).id,
				                 					 listCompany.get(i).person.id,
					                  			 listCompany.get(i).person.code,
					                  			 listCompany.get(i).person.name, 
					                  			 listCompany.get(i).person.cpfCnpj
				                 });
		}
		
		protected override void find()
		{
			common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
		}
		
		protected override void init()
		{
			DataGridViewTextBoxColumn companyId = new DataGridViewTextBoxColumn();
			
			companyId.Visible = false;
			companyId.Name  = "companyId";
			
			dgvMain.Columns.Add(companyId);	
			
			base.init();
		}
		
		protected override void newEvent()
		{
			company.clear();
			base.newEvent();
		}
		
		protected override bool save()
		{
			common.Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
			if (!base.save())
			{
				common.Database.RollBack();
				return false;
			}
			if (!company.save())
			{
				common.Database.RollBack();
				return false;
			}
			common.Database.Commit();
			return true;
		}
		
		protected override void setClass()
		{
			base.setClass();
		}
		
		protected override void setEdits()
		{
			company.id = dgvMain.CurrentRow.Cells["companyId"].Value.ToString();
			
			if (company.get())
			{
				base.setEdits();
			}
		}
		
		protected override bool update()
		{
			if (!base.update())
				return false;
			return company.update();
		}
		
		#endregion
		
	}
}
